package lt.marsrutai.mobile.android.adapter;

import java.util.Map;

import lt.marsrutai.mobile.android.AppContext;
import lt.marsrutai.mobile.android.R;
import lt.marsrutai.mobile.android.model.Schedule;
import lt.marsrutai.mobile.android.model.Track;
import lt.marsrutai.mobile.android.model.TrackBookmark;
import lt.marsrutai.mobile.android.model.enums.TransportType;
import lt.marsrutai.mobile.android.widget.ListViewAdapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class TrackBookmarkAdapter extends ListViewAdapter<TrackBookmark> {

    private Map<TrackBookmark, Track> tracks;
    private Map<TrackBookmark, Schedule> schedules;

    public TrackBookmarkAdapter(Context context, Map<TrackBookmark, Track> tracks, Map<TrackBookmark, Schedule> schedules) {
        super(context, R.layout.track_bookmark_item);

        this.tracks = tracks;
        this.schedules = schedules;
    }

    @Override
    public View doGetView(int position, View view, ViewGroup parent) {

        TrackBookmark trackBookmark = getItem(position);
        Track track = tracks.get(trackBookmark);
        Schedule schedule = schedules.get(trackBookmark);

        if (trackBookmark != null) {
            TextView scheduleNumber = (TextView) view.findViewById(R.id.schedule_number);
            if (scheduleNumber != null) {
                if (!TransportType.TRAIN.equals(schedule.getTransportType())) {
                    scheduleNumber.setText(schedule.getName());
                }
                scheduleNumber.setBackgroundColor(AppContext.getTransportInfoResolver().resolveTransportColor(trackBookmark.getScope(), schedule.getTransportId()));
            }

            TextView trackName = (TextView) view.findViewById(R.id.track_name);
            if (trackName != null) {
                trackName.setText(track.getName());
            }
        }
        return view;
    }
}
